//
//  RecommendViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RecommendViewController.h"
#import "RecreationDPViewController.h"
#import "PhoneixNews.pch"

@interface RecommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mutableArray;//内容数组
@end

@implementation RecommendViewController
- (void)dealloc {
    [_tableView release];
    [_mutableArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 155) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[HeadlineTableViewCell class] forCellReuseIdentifier:@"cell"];
//    [self.tableView registerClass:[HeadlineThreeImageTableViewCell class] forCellReuseIdentifier:@"cellThreeImage"];
    [self.tableView registerClass:[RecommentTableViewCell class] forCellReuseIdentifier:@"recomment"];
    [self.view addSubview:_tableView];
    [_tableView release];
    [self getData];
    
}
#pragma mark -解数剧
- (void)getData {
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:Recommend parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = dic[@"item"];
        self.mutableArray = [HeadlineModel modelHanderWithArray:array];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - TableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mutableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_mutableArray.count != 0) {
        HeadlineModel *headline = _mutableArray[indexPath.row];
        if (headline.thumbnail != nil) {
            HeadlineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            
            
            [cell.TitleImage sd_setImageWithURL:[NSURL URLWithString:headline.thumbnail]];
            cell.title.text = headline.title;
            cell.comments.text = headline.commentsall;
            cell.Commentsimage.image = [UIImage imageNamed:@"imageComments"];
            cell.time.text = [headline.updateTime substringWithRange:NSMakeRange(10, 6)];
            return cell;

    }else if (headline.thumbnail == nil){
        RecommentTableViewCell *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"recomment"];
        cellTwo.title.text = headline.title;
        return cellTwo;
    }else {
        HeadlineThreeImageTableViewCell *cellThreeImage = [tableView dequeueReusableCellWithIdentifier:@"cellThreeImage"];
        
        [cellThreeImage.Threeimage sd_setImageWithURL:[NSURL URLWithString:headline.images[0]]];
        [cellThreeImage.ThreeimageTwo sd_setImageWithURL:[NSURL URLWithString:headline.images[1]]];
        [cellThreeImage.ThreeimageThree sd_setImageWithURL:[NSURL URLWithString:headline.images[2]]];
        cellThreeImage.title.text = headline.title;
        cellThreeImage.time.text = [headline.updateTime substringWithRange:NSMakeRange(10, 6)];
        cellThreeImage.comments.text = headline.commentsall;
        cellThreeImage.Commentsimage.image = [UIImage imageNamed:@"imageComments"];
        return cellThreeImage;

       }
     }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeadlineModel *headline = _mutableArray[indexPath.row];
    if (headline.thumbnail != nil) {
        return 130;
    }else {
        return 240;
    }
    
}
#pragma mark - Cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HeadlineModel *headline = _mutableArray[indexPath.row];
    RecreationDPViewController *recreation = [[RecreationDPViewController alloc] init];
    recreation.UrlStr = headline.url;
    [self.navigationController pushViewController:recreation animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
